% # File Info -- stSpec.sty, Part of SDUSThesis package, layout settings.

% Version Control System Information -- Subversion, hosts on <+Google Code+>;
% $URL: http://sdusthesis.googlecode.com/svn/trunk/stSpec.sty $;
% $Id: stSpec.sty 23 2012-05-13 14:46:11Z suxpert@gmail.com $;

% # Copyright (C) 2009-2012, LiTuX, All wrongs reserved.
%
%	Permission is granted to copy, distribute and/or modify this document
%	under the terms of the GNU Free Documentation License, Version 1.3
%	or any later version published by the Free Software Foundation;
%	with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
%	A copy of the license is included in the section entitled "GNU
%	Free Documentation License".
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{stSpec}[2012/05/13 v0.5 SDUST-Thesis Specification by LiTuX]

\RequirePackage{ifthen}
\RequirePackage{etoolbox}
\RequirePackage{ifxetex}
\RequirePackage{xstring}

\newtoggle{STabstract}
\DeclareOption{abstract}{\toggletrue{STabstract}}
\DeclareOption{thesis}{\togglefalse{STabstract}}

\DeclareOption*{%
	\ClassWarning{SDUSThesis}{Unknown option `\CurrentOption'}%
}
\ProcessOptions\relax

% \RequirePackage{xcolor}

% \RequirePackage{fontspec}
\RequirePackage{mathptmx} % libertineotf
\RequirePackage[T1]{fontenc}
\DeclareRobustCommand\nobreakspace{\leavevmode\nobreak\ }
% \setmainfont[
% Ligatures=TeX,
% BoldFont={Times New Roman Bold},
% Mapping=tex-text,
% ]{Times New Roman} % TeX Gyre Termes?

\RequirePackage{titlesec, titletoc} % TODO
\titlecontents{chapter}[0pt]
	{\vspace{0.25\baselineskip} \bfseries \large}
	{\def\numberline##1{\thecontentslabel\quad\ignorespaces}}
	{}
	{\dotfill\contentspage}
	% {\titlerule*[0.5pc]{.}\contentspage}

\titlecontents{section}[2.3em] % ie, 1.5em (chapter) + 2.3em
	{}
	% {\contentslabel{2.3em}}
	{\def\numberline##1{\thecontentslabel\quad\ignorespaces}}
	{\hspace*{-2.3em}}
	{\dotfill\contentspage}

\RequirePackage{graphicx}
\RequirePackage{amsmath, amssymb, amsfonts, amsthm}

\RequirePackage{ccaption}
\captionstyle{\centering}
\precaption{\postcaption{}}
\midbicaption{\precaption{}\postcaption{\vspace{10pt}}}

\newcommand{\captions}[2]{%
	\ifx\@captype\undifined%
		\ClassWarning{SDUSThesis}{\noexpand\captions outside float}%
	\else%
		\bicaption{}{#1}{%
			\csname en\@captype name\endcsname%
		}{#2}%
	\fi%
}

\RequirePackage[
	sorting=none,
	bibstyle=sdust,
	minnames=3,
	language=chinese,
	hyperref=true,
]{biblatex}

\defbibheading{bibliography}[\bibname]{%
	\chapter{#1}%
	\echapter{Bibliography}%
	% \markboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
	% \addcontentsline{toc}{chapter}{\bibname}%
	% \addcontentsline{toe}{chapter}{Bibliography}%
}

\RequirePackage{hyperref}
\hypersetup{
	% bookmarks=false,
	% pdfencoding=unicode,
	unicode=true,
	colorlinks=true,
	breaklinks=true,
	% citecolor=green,
	% filecolor=brown,
	% linkcolor=red,
	% urlcolor=blue,
	bookmarksnumbered=true,
	bookmarksopen=false,
	% plainpages=true,
	pdfhighlight=/P, % /I(inverse) /N(no effect) /O(outline) /P(inset)
	% pdfauthor={\zh@author},
	% pdftitle={\zh@title},
	% pdfsubject={\schoolName\thesisType},
	% pdfkeywords={},
	pdfstartview=FitH, %FitBH, FitB
	pdfview=FitH,
	% pdfborder={0 0 0},
	pdfpagemode=UseOutlines, %None, FullScreen, UseThumbs
	% xetex,	% default when use xelatex.
}

\ifthenelse{\boolean{xetex}}{\def\zhclen{1}}{\def\zhclen{3}}

%%%%%%%%%%%%%%%%%%%%%%%  Settings: %%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{fancyhdr}
\iftoggle{STabstract}{% abstract
\pagestyle{fancy}
	\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
	\fancyhf{}% clear all header and footer fields
	\fancyhead[L]{\small\schoolName\@degreeLevel\thesisType}%
	\fancyhead[R]{\small\leftmark}%
	\fancyfoot[C]{\small\thepage}%

\fancypagestyle{headings}{%
	\fancyhf{}% clear all header and footer fields
	\fancyhead[L]{\small\schoolName\@degreeLevel\thesisType}%
	\fancyhead[R]{\small\leftmark}%
}
}{% thesis
\pagestyle{fancy}
	\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
	\fancyhf{}% clear all header and footer fields
	\fancyhead[LO, RE]{\small\schoolName\@degreeLevel\thesisType}%
	\fancyhead[RO, LE]{\small\leftmark}%
	\fancyfoot[C]{\small\thepage}%

\fancypagestyle{headings}{%
	\fancyhf{}% clear all header and footer fields
	\fancyhead[LO, RE]{\small\schoolName\@degreeLevel\thesisType}%
	\fancyhead[RO, LE]{\small\leftmark}%
}
}% end iftoggle

\fancypagestyle{plain}{%
	\pagestyle{fancy}%
}
% \fancypagestyle{plain}{%
% 	\fancyhf{}% clear all header and footer fields
% 	\fancyhead[L]{\small\schoolName\@degreeLevel{}论文}%
% 	\fancyhead[R]{\small\leftmark}%
% 	\fancyfoot[C]{\small\thepage}%
% }

\CTEXsetup[beforeskip=10pt]{chapter}
\CTEXsetup[afterskip=15pt]{chapter}
\CTEXsetup[name={,}]{chapter}
\CTEXsetup[number=\arabic{chapter}]{chapter}
% \CTEXsetup[nameformat={\ziju{1}\zihao{-2}\songti\bfseries}]{chapter}
% \CTEXoptions[contentsname={目录}]

\renewcommand{\CJKunderlinecolor}{}
\renewcommand{\ULthickness}{0.7pt}

\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{3}

%%%%%%%%%%%%%%%%%%%%%%% Definition %%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\connSep}{：\kern-0.2em}
\newcommand{\classNoName}{分类号}
\newcommand{\secTypeName}{密级}
\newcommand{\UDCName}{U~D~C}
\newcommand{\orgNoName}{单位代码}
\newcommand{\theType}{学位}
\newcommand{\thesisType}{\theType 论文}
\newcommand{\schoolName}{山东科技大学}
\newcommand{\degreeLevelName}{申请学位级别}
\newcommand{\majorName}{专业名称}
\newcommand{\supervisorName}{指导教师姓名}
\newcommand{\profTitleName}{职称}
\newcommand{\titleName}{论文题目}
\newcommand{\authorName}{作者姓名}
\newcommand{\superName}{指导教师}
\newcommand{\fieldName}{研究方向}
\newcommand{\enrollDate}{入学时间}
\newcommand{\submitDate}{论文提交日期}
\newcommand{\defenseDate}{论文答辩日期}
\newcommand{\awardDate}{授予学位日期}
\newcommand{\zhAbstractName}{摘要}
\newcommand{\enAbstractName}{Abstract}

\newcommand{\zhDetailedAbstraceName}{详细摘要}
\newcommand{\enDetailedAbstraceName}{Detailed Abstract}

\newcommand{\acknowledgementName}{致谢}
\newcommand{\acknowledgementNameEn}{Acknowledgement}

\newcommand{\affirmationName}{声明}
\newcommand{\affirmationNameEn}{Affirmation}
\newcommand{\zhKeywordName}{关键词}
\newcommand{\enKeywordName}{Keywords}

\newcommand{\schoolNameEn}{Shandong University of Science and Technology}
\newcommand{\superNameEn}{Supervisor}

\newcommand\engcontentsname{Contents}
\newcommand{\entablename}{Table}
\newcommand{\enfigurename}{Fig.}

%%%%%%%%%%%%%%%%%%%%%%% Commands %%%%%%%%%%%%%%%%%%%%%%%
% SDUS{\sffamily T}{\scshape hesis}%
\newcommand{\SDUSThesis}{%
	{\scshape SDUSThesis}%
}
% \newcommand{\expand}[2][4em]{\makebox[#1][s]{#2}}
% \newcommand{\cexpand}[2][4em]{%
% \makebox[#1][s]{\hspace*{\fill}#2\hspace*{\fill}}}
\newcommand{\expand}[2][4em]{%
	\begin{CJKfilltwosides}{#1}%
	#2%
	\end{CJKfilltwosides}%
}

\newcommand{\cexpand}[2][4em]{%
	\begin{CJKfilltwosides}{#1}%
	\hfill#2\hfill%
	\end{CJKfilltwosides}%
}

\renewcommand{\author}[2][]{%
   \def\zh@author{#2}%
   \ifthenelse{\equal{#1}{}}{%
      \def\en@author{#2}%
   }{%
      \def\en@author{#1}%
   }%
}

\renewcommand{\title}[2][]{%
   \def\zh@title{#2}%
   \ifthenelse{\equal{#1}{}}{%
      \def\en@title{#2}%
   }{%
      \def\en@title{#1}%
   }%
}

\newcommand{\cNumber}[1]{%
   \CTEXnumber{\@tmpVar}{#1}\@tmpVar%
}
\newcommand{\cDigit}[1]{%
   \CTEXdigits{\@tmpVar}{#1}\@tmpVar%
}

\newcommand{\formatsetting}[1]{%
   \IfStrEqCase{#1}{%
      {short}{\def\format{short}}%
      {long}{\def\format{long}}%
      {big}{\def\type{big}}%
      {small}{\def\type{small}}%
      {old}{\def\type{old}}%
   }[\ClassWarning{SDUSThesis}{Unknown argument: `#1'}]%
}

\newcommand{\strParse}[1]{%
   \def\strParseFlag{0}% 0 is OK
   \StrSubstitute{#1}{/}{.}[\dateStr]%
   \StrSubstitute{\dateStr}{-}{.}[\dateStr]%
   \StrCount{\dateStr}{.}[\dotNum]% how many dots
   \IfEqCase{\dotNum}{% cases of dots
      {2}{% 2011.12.31 like
         \def\dateformat{long}%
         \StrBefore{\dateStr}{.}[\theYear]%
         \StrBetween[1, 2]{\dateStr}{.}{.}[\theMonth]%
         \StrBehind[2]{\dateStr}{.}[\theDay]%
      }{1}{% 2011.12 like, `long` will not support
         \def\dateformat{short}%
         \StrBefore{\dateStr}{.}[\theYear]%
         \StrBehind{\dateStr}{.}[\theMonth]%
      }% cases
   }[% default, wrong string
   % \def\dateformat{wrong}%
   \def\strParseFlag{1}% 0 is OK, 1 is error
   % \ClassWarning{SDUSThesis}{Unrecognized date.}%
   % \def\theYear{\the\year}%
   % \def\theMonth{\the\month}%
   % \def\theDay{\the\day}%
   ]% end case
}

\newcommand{\convertDate}[4][short, small]{% default is small short
%%%%%%%%%%%%%%% |#1|, |#2|, |#3|, |#4|: 
   \def\format{short}% default
   \def\type{small}% default
   \StrDel{#1}{ }[\formats]%
   \StrCount{\formats}{,}[\commaNum]%
   \IfEqCase{\commaNum}{% cases of commas, format settings
      {1}{% short, small like
         \StrBefore{\formats}{,}[\parta]%
         \StrBehind{\formats}{,}[\partb]%
         \formatsetting{\parta}%
         \formatsetting{\partb}%
      }{0}{% short like, or small like
         \formatsetting{\formats}%
      }% cases
   }[% default, wrong string
      \ClassWarning{SDUSThesis}{Wrong arguments: `\formats'}%
   ]% end case.
   \ifthenelse{\equal{#4}{}}{\def\format{short}}{}%
   \IfStrEqCase{\type}{% for different types
      {big}{% Chinese big
         \cDigit{#2}年\cNumber{#3}月%
         \IfStrEq{\format}{long}{\cNumber{#4}日}{}%
      }{small}{% Chinese small, with numbers
         #2 年{} #3 月%
         \IfStrEq{\format}{long}{ #4 日}{}%
      }% cases
   }[% fallback to old
      \ifcase#3\or January\or February\or March\or April\or May\or June\or
      July\or August\or September\or October\or November\or December\fi%
      \IfStrEq{\format}{long}{ #4}{}%
      , #2%
   ]% end cases
}

\newcommand{\showDate}[2][short, small]{% default is small short.
   \strParse{#2}% Get the date value
   \ifthenelse{\strParseFlag=1}{% unrecognized string
      \ClassWarning{SDUSThesis}{Unrecognized date, use it directly.}%
      #2% show the string directly.
   }{% else, convert it.
      \IfStrEq{\dateformat}{short}{%
         \convertDate[#1]{\theYear}{\theMonth}{}%
      }{%
         \convertDate[#1]{\theYear}{\theMonth}{\theDay}%
      }% end short
   }% end wrong
}

\renewcommand{\date}[1]{
   \def\submit@date{#1}%
   \strParse{#1}% Get the date value
   \ifthenelse{\strParseFlag=1}{% unrecognized
      \ClassWarning{SDUSThesis}{Unrecognized date, use `\protect\today'.}%
      \edef\submit@year{\the\year}%
      \edef\submit@month{\the\month}%
      \edef\submit@day{\the\day}%
   }{% else
      \edef\submit@year{\theYear}%
      \edef\submit@month{\theMonth}%
      \IfStrEq{\dateformat}{short}{% If short, day may be unused
         \edef\submit@day{1}% use 1st of the month
      }{% long, use the day value.
         \edef\submit@day{\theDay}%
      }% end short test.
   }% end wrong
   \edef\default@enroll{\number\numexpr\submit@year-3\relax}% default setting.
   \ifthenelse{\submit@year=\the\year}{}{%
      \ClassWarning{SDUSThesis}{Are you sure it's \submit@year
      but not \the\year?}%
   }% end check value
}

\newcommand{\useDate}[1]{%
   \convertDate[#1]{\submit@year}{\submit@month}{\submit@day}%
}

\newcommand{\maxwide}[2]{%
   \StrLen{#1}[\llen]%
   \StrLen{#2}[\rlen]%
   %%%%%%%% left: \llen, right: \rlen.
   \ifthenelse{\llen>\rlen}{% left is longer
      \def\thewide{\number\numexpr\llen/\zhclen}%
   }{% right is longer
      \def\thewide{\number\numexpr\rlen/\zhclen}%
   }% end ifthen
}

\newcommand{\checkwide}[1]{%
   \ifthenelse{\thewide<4}{% too short
      \def\thewide{4}% use 4 as minimum
   }{% else
      \ifthenelse{\thewide>7}{% too long
         \ClassWarning{SDUSThesis}{Fixme: string too long to adjust.}%
      }{}% end 7 test
   }% end 4 test
}

\newcommand{\strDiv}[1]{%
   %%%%%%% input #1, |\meaning#1|:\\
   \saveexpandmode\expandarg% change mode
   \IfSubStr*{#1}{\string\\}{% `\\' force break
      %%%%%%% found;
      \StrDel{#1}{ }[\nospacestr]% remove spaces
      \StrBefore*{\nospacestr}{\string\\}[\lpart]% left part
      \StrBehind*{\nospacestr}{\string\\}[\rpart]% right part
      \maxwide{\lpart}{\rpart}%
      \checkwide{\thewide}% wide result
      \def\lines{2}% linebreak flag
   }{% no `\\' force break
      \StrCount{#1}{ }[\spcount]% check how many spaces
      \ifthenelse{\spcount=1}{% only one space, treat as line break
         %%%%%%% space separate;
         \StrBefore{#1}{ }[\lpart]% left part
         \StrBehind{#1}{ }[\rpart]% right part
         \maxwide{\lpart}{\rpart}%
         \checkwide{\thewide}% wide result
         \def\lines{2}% linebreak flag
      }{% more than one, or none, ignore them.
         \StrDel{#1}{ }[\nospacestr]% remove spaces
         \StrLen{\nospacestr}[\slen]% whole length
         \def\thelen{\number\numexpr\slen/\zhclen\relax}% convert
         % \def\testnum{\number\numexpr\slen/\zhclen/2\relax}
         %%%%%%%%%%%% not found, |\meaning\nospacestr|, \slen/\zhclen, \thelen;
         \ifthenelse{\thelen<4}{% too short, do not break
            \def\lines{1}% only one line
            \def\thewide{4}% 4 is the minimum
         }{% > 4
            \ifthenelse{\thelen>7}{% long, break in the middle
               \def\thewide{\number\numexpr\thelen/2\relax}%
               \StrSplit{\nospacestr}{\numexpr\thewide*\zhclen\relax}{\lpart}{\rpart}% split
               \checkwide{\thewide}%
               \def\lines{2}% linebreak flag
            }{% 4 \to 7, use the length as wide value
               \def\lines{1}%
               \edef\thewide{\thelen}%
            }% end 7
         }% end 4
      }% end space test
   }% end `\\' test.
   %%%%%%%% \fbox{\lpart}\fbox{\rpart}\\
   %%%%%% |\meaning\lpart|\StrLen{\lpart}, |\meaning\rpart|\StrLen{\rpart}.
   \restoreexpandmode%
}

\newcommand{\aexpand}[1]{%
   % \def\stringin{#1}%
   % \strDiv{\stringin}%
   \strDiv{#1}%
   %%%%%%%%%%%% wide: \thewide.
   \ifthenelse{\lines=2}{% have line break
      \begin{minipage}[t]{\thewide\ccwd}
         \CJKunderline{\cexpand[\thewide\ccwd]{\lpart}}\\
         \CJKunderline{\cexpand[\thewide\ccwd]{\rpart}}
      \end{minipage}
   }{% only one line
      \CJKunderline{\cexpand[\thewide\ccwd]{\nospacestr}}
   }% end ifthen
}

\newcommand{\classNo}[1]{%
\ifthenelse{ \equal{#1}{} }%
	{\def\@classNo{\space}}%
	{\def\@classNo{#1}}%
}
\newcommand{\secType}[1]{%
\ifthenelse{ \equal{#1}{} }%
	{\def\@secType{\space}}%
	{\def\@secType{#1}}%
}
\newcommand{\UDC}[1]{%
\ifthenelse{ \equal{#1}{} }%
	{\def\@UDC{\space}}%
	{\def\@UDC{#1}}%
}
\newcommand{\orgNo}[1]{%
\ifthenelse{ \equal{#1}{} }%
	{\def\@orgNo{\space}}%
	{\def\@orgNo{#1}}%
}

\newcommand{\rmsp}[1]{%
	\saveexpandmode\expandarg%
	\StrCount{#1}{ }[\spcount]%
	\ifthenelse{ \spcount=0 }%
		{\def\rmspStr{#1}}%
		{\StrDel{#1}{ }[\rmspStr]}%
	\restoreexpandmode%
}

\newcommand{\degree}[2]{%
\ifthenelse{ \equal{#1}{} }%
	{\def\type@degree{\space}}%
	{\def\type@degree{#1}}%
\ifthenelse{ \equal{#2}{} }%
	{\def\@degreeLevel{\space}}%
	{\rmsp{#2}%
	\def\@degreeLevel{\rmspStr}%
	}%
}

\newcommand{\college}[2]{%
   \ifthenelse{\equal{#1}{}}{%
      \def\zh@college{\space}}{%
      \def\zh@college{#1}%
   }%
   \ifthenelse{\equal{#2}{}}%
      {\def\en@college{\space}}%
      {\def\en@college{#2}}%
}


\newcommand{\major}[1]{%
   \def\input@major{#1}%
   \strDiv{\input@major}%
   \edef\majorWidth{\expandonce{\thewide}}%
   \edef\majorBreak{\expandonce{\lines}}%
   \ifthenelse{\lines=2}{%
      \edef\l@major{\expandonce{\lpart}}\edef\r@major{\expandonce{\rpart}}%
   }{%
      \edef\@major{\expandonce{\nospacestr}}%
   }% end ifthen
}
\newcommand{\supervisor}[2][]{%
   \ifthenelse{\equal{#2}{}}{%
      \def\zh@supervisor{\space}%
   }{%
      \def\zh@supervisor{#2}%
   }%
   \ifthenelse{\equal{#1}{}}%
      {\def\en@supervisor{#2}}%
      {\def\en@supervisor{#1}}%
   \def\supervisorList{%
      \CJKunderline{\cexpand[\majorWidth\ccwd]{\zh@supervisor}}%
   }%
}
\newcommand{\ptitle}[2]{%
   \ifthenelse{\equal{#1}{}}{%
      \def\zh@ptitle{\space}}{%
      \def\zh@ptitle{#1}%
   }%
   \ifthenelse{\equal{#2}{}}%
      {\def\en@ptitle{\space}}%
      {\def\en@ptitle{#2}}%
   \def\personTitleList{%
      \CJKunderline{\cexpand[\rightWidth\ccwd]{\zh@ptitle}}%
   }%
}

% an ugly command:
\newcommand{\advisor}[2]{%
   \appto{\supervisorList}{%
      \\\CJKunderline{\cexpand[\majorWidth\ccwd]{#1}}%
   }%
   \appto{\personTitleList}{%
      \\\CJKunderline{\cexpand[\rightWidth\ccwd]{#2}}%
   }%
}

\newcommand{\enroll}[1]{%
   \def\enroll@date{#1}%
   \strParse{#1}%
   \ifthenelse{\strParseFlag=1}{% unrecognized
      \ClassWarning{SDUSThesis}{Unrecognized date, use default.}%
      \edef\enroll@year{\default@enroll}%
      \edef\enroll@month{9}%
      \edef\enroll@day{3}%
   }{% else
      \edef\enroll@year{\theYear}%
      \edef\enroll@month{\theMonth}%
      \IfStrEq{\dateformat}{short}{% If short, day may be unused
         \edef\enroll@day{3}% use 3rd of the month
      }{% long, use the day value.
         \edef\enroll@day{\theDay}%
      }% end short test.
   }% end wrong
}
\newcommand{\field}[1]{%
   \def\input@field{#1}%
   \strDiv{\input@field}%
   \edef\fieldWidth{\expandonce{\thewide}}%
   \edef\fieldBreak{\expandonce{\lines}}%
   \ifthenelse{\lines=2}{%
      \edef\l@field{\expandonce{\lpart}}\edef\r@field{\expandonce{\rpart}}%
   }{%
      \edef\@field{\expandonce{\nospacestr}}%
   }% end ifthen
   \ifthenelse{\fieldWidth<6}{\def\rightWidth{6}}{\edef\rightWidth{\fieldWidth}}%
}

\newcommand{\defense}[1]{
   \def\defense@date{#1}%
   \strParse{#1}% Get the date value
   \ifthenelse{\strParseFlag=1}{% unrecognized
      \ClassWarning{SDUSThesis}{Unrecognized date, use default.}%
      \def\defense@year{\submit@year}%
      \def\defense@month{6}%
      \def\defense@day{17}%
   }{% else
      \edef\defense@year{\theYear}%
      \edef\defense@month{\theMonth}%
      \IfStrEq{\dateformat}{short}{% If short, day may be unused
         \def\defense@day{17}%
      }{% long, use the day value.
         \edef\defense@day{\theDay}%
      }% end short test.
   }% end wrong
}

\newcommand{\showMajor}{%
   \ifthenelse{\majorBreak=2}{% have line break
      \begin{minipage}[t]{\majorWidth\ccwd}
         \CJKunderline{\cexpand[\majorWidth\ccwd]{\l@major}}\\
         \CJKunderline{\cexpand[\majorWidth\ccwd]{\r@major}}
      \end{minipage}
   }{% only one line
      \CJKunderline{\cexpand[\majorWidth\ccwd]{\@major}}%
   }% end ifthen
}
\newcommand{\showField}{%
   \ifthenelse{\fieldBreak=2}{% have line break
      \begin{minipage}[t]{\rightWidth\ccwd}
         \CJKunderline{\cexpand[\rightWidth\ccwd]{\l@field}}\\
         \CJKunderline{\cexpand[\rightWidth\ccwd]{\r@field}}
      \end{minipage}
   }{% only one line
      \CJKunderline{\cexpand[\rightWidth\ccwd]{\@field}}%
   }% end ifthen
}

%%%%%%%%%%%%%%%%%%%%%  %%%%%%%%%%%%%%%%%%%%%%
\newcommand{\stfrontmatter}{%
% \frontmatter% will leave a blank page if the next page is not odd.
	\iftoggle{STabstract}{%
		\@mainmattertrue%
		\pagenumbering{arabic}%
	}{%
		\@mainmatterfalse%
		\pagenumbering{roman}%
	}%
	\loadgeometry{stBody}%
}

\newcommand{\stmainmatter}{%
	\iftoggle{STabstract}{%
		% do nothing
	}{%
		\mainmatter%
		%\@mainmattertrue
		%\pagenumbering{arabic}
		% \pagestyle{fancy}
		% \CTEXsetup[titleformat={\zihao{-3}\songti\bfseries}]{chapter}
	}%
}
\newcommand{\stappendix}{%
	\@mainmattertrue%
	\appendix%
}

% An ugly way for ignore-all-to-the-end.
\iftoggle{STabstract}{%
	\def\stbackmatter{\end{document}}%
}{%
	\newcommand{\stbackmatter}{%
		\backmatter%
	}%
}

\renewcommand{\today}{\the\year.\the\month.\the\day}

%%%%%%%%%%%%%%%%%%%%%%% Environments: %%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{zhAbstract}{
	\iftoggle{STabstract}{}{\thispagestyle{headings}}
	\chaptermark{\zhAbstractName}
	\pdfbookmark[0]{\zhAbstractName}{zhAbstract}
	\begin{center}
		\vspace*{1.5em}
		{\songti\zihao{-2}\bfseries\ziju{1} \zhAbstractName}
		\vspace{1.5em}
	\end{center}
	\par
}% before
{
	\iftoggle{STabstract}{\clearpage}{%
		\newpage\thispagestyle{empty}\cleardoublepage%
	}
}% after

\newcommand{\zhKeywords}[1]{%
	\vspace{1.5em}%
	{\bfseries \zhKeywordName{}：}#1%
}

\newenvironment{enAbstract}{
	\iftoggle{STabstract}{}{\thispagestyle{headings}}
	\chaptermark{\enAbstractName}
	\pdfbookmark[0]{\enAbstractName}{enAbstract}
	\begin{center}
		\vspace*{1.5em}
		{\Large \bfseries \enAbstractName}
		\vspace{1.5em}
	\end{center}
	\par
}% before
{
	\iftoggle{STabstract}{\clearpage}{%
		\newpage\thispagestyle{empty}\cleardoublepage%
	}
}% after

\newcommand{\enKeywords}[1]{%
	\vspace{1.5em}%
	{\bfseries \enKeywordName{}: }#1%
}

\iftoggle{STabstract}{
%% define the detailed abstract environment
\newenvironment{zhDetailed}{
	\chaptermark{\zhDetailedAbstraceName}
	\pdfbookmark[0]{\zhDetailedAbstraceName}{zhDetailed}
	\begin{center}
		\vspace*{1.5em}
		{\songti\zihao{-2}\bfseries\ziju{1} \zhDetailedAbstraceName}
		\vspace{1.5em}
	\end{center}
	\par
}% before
{\clearpage}% after

\newenvironment{enDetailed}{
	\chaptermark{\enDetailedAbstraceName}
	\begin{center}
		\vspace*{1.5em}
		\pdfbookmark[0]{\enDetailedAbstraceName}{enDetailed}
		{\Large \bfseries \enDetailedAbstraceName}
		\vspace{1.5em}
	\end{center}
	\par
}% before
{\clearpage}% after
}{}% end iftoggle

\newenvironment{acknowledgement}{
	\chapter{\acknowledgementName}
	\echapter{\acknowledgementNameEn}
}% before
{\clearpage}% after

% \newenvironment{works}{
% }% before
% {}% after

%%%%%%%%%%%%%%%%%%% English Contents %%%%%%%%%%%%%%%%%%%
\newcommand\tableofengcontents{%
	\if@twocolumn
		\@restonecoltrue\onecolumn
	\else
		\@restonecolfalse
	\fi
	\chapter*{\engcontentsname}%
	\@mkboth{\MakeUppercase\engcontentsname}{\MakeUppercase\engcontentsname}%
	\@starttoc{toe}% !!!!Define a new contents!!!!
	\if@restonecol\twocolumn\fi
}

\newcommand\addengcontents[2]{%
	\if@mainmatter
    	\addcontentsline{toe}{#1}%
			{\protect\numberline{\csname the#1\endcsname}#2}%
	\else
    	\addcontentsline{toe}{#1}{#2}%
	\fi
}

\newcommand\echapter[1]{\addengcontents{chapter}{#1}}
\newcommand\esection[1]{\addengcontents{section}{#1}}
\newcommand\esubsection[1]{\addengcontents{subsection}{#1}}


\iftoggle{STabstract}{% abstract
\newcommand{\makecontents}{%
	% do nothing because we do not need it.
}%
}{% thesis
\newcommand{\makecontents}{%
	\clearpage{%
		\pagestyle{headings}%
		\pdfbookmark[0]{\contentsname}{zhcontents}%
		\tableofcontents%
		\clearpage\thispagestyle{empty}\cleardoublepage%
		\pdfbookmark[0]{\engcontentsname}{encontents}%
		\tableofengcontents%
		\clearpage\thispagestyle{empty}\cleardoublepage%
	}%
}% two contents
}% end iftoggle

%%%%%%%%%%%%%%%%%% default settings: %%%%%%%%%%%%%%%%%%%%
\classNo{}
\secType{公开}
\UDC{}
\orgNo{10424}
\degree{Science}{硕士}
\major{对应专业名称}
\field{专业研究方向}
\supervisor[Supervisor Name]{导师姓名}
\ptitle{教授}{Prof.~}
\college{你的学院}{Input your College name here}

\date{\today}
\enroll{\default@enroll.9.3}
\defense{\submit@year.6.17}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\AtBeginDocument{
	\loadgeometry{stFront}
	\pagenumbering{Roman}
}

\newcommand{\infoType}[1]{{\zihao{-4}\songti\bfseries{#1}}}
\newcommand{\zhHeadType}[1]{{\zihao{3}\kaishu\bfseries{#1}}}
\newcommand{\fillType}[1]{{\zihao{3}\songti\bfseries{#1}}}

%%%%%%%%%%%%%%%%%%%%%%% TitlePage %%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\makecover}[1]{
\thispagestyle{empty}
\pdfbookmark[-1]{\zh@title}{cover}
\begin{center}
	\infoType{%
		\expand[4em]{\classNoName}\connSep%
		\CJKunderline{\cexpand[4em]{\@classNo}}%
		\hfill%
		\expand[4em]{\secTypeName}\connSep%
		\CJKunderline{\cexpand[4em]{\@secType}}%
		\\%
		\cexpand[4em]{\UDCName}\connSep%
		\CJKunderline{\cexpand[4em]{\@UDC}}%
		\hfill%
		\expand[4em]{\orgNoName}\connSep%
		\CJKunderline{\cexpand[4em]{\@orgNo}}%
		\\%
	}

	\vspace{5em}

	{\ziju{.5} \zihao{-2} \songti \bfseries #1}
	\vspace{-1em}
	\begin{center}
		\zihao{1} \heiti \bfseries
		% \linespread{1.2}\selectfont
		\zh@title
	\end{center}
	\vfill
	{\zihao{2} \songti \bfseries \cexpand[4em]{\zh@author}}
	\vfill

	{\zihao{-2}\songti\bfseries%
		\hfill%
		\scalebox{0.8}[1]{\expand[6em]{\degreeLevelName}}\connSep%
		\CJKunderline{\cexpand[4em]{\@degreeLevel\theType}}%
		\hspace{\stretch{2}}%
		\scalebox{0.8}[1]{\expand[4em]{\majorName}}\connSep%
		\showMajor%
		%\hfill%\\[.5em]%
		\hspace*{\fill}%
		\par%
		\vspace{0.5em}%
		\hfill%
		\scalebox{0.8}[1]{\expand[6em]{\supervisorName}}\connSep%
		\CJKunderline{\cexpand[4em]{\zh@supervisor}}%
		\hspace{\stretch{2}}%
		\scalebox{0.8}[1]{\expand[4em]{\profTitleName}}\connSep%
		\CJKunderline{\cexpand[\majorWidth\ccwd]{\zh@ptitle}}%
		\hspace*{\fill}%
	}

	\vspace{\stretch{2}}
	{\ziju{1} \zihao{2} \songti \bfseries \schoolName}\\[.8em]
	{\zihao{-2} \songti \bfseries \useDate{big}}
\end{center}

\iftoggle{STabstract}{\clearpage}{%
	\newpage\thispagestyle{empty}\cleardoublepage%
}
}% makecover

%%%%%%%%%%%%%%%%%%%%%%% TitlePage %%%%%%%%%%%%%%%%%%%%%%%
\renewcommand{\maketitle}{%
	\hypersetup{
		pdfauthor={\zh@author},
		pdftitle={\zh@title},
		pdfsubject={\schoolName\@degreeLevel\thesisType},
	}%
	\iftoggle{STabstract}{%
		\makecover{\thesisType\zhDetailedAbstraceName}%
		\makeflypage%
	}{%
		\makecover{\thesisType}%
		\makeflypage%
		\affirmation%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%% HeadPage %%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\makeflypage}{
%%%%%%%%%%%%%%%%%%%%%%% Chinese %%%%%%%%%%%%%%%%%%%%%%%%
\thispagestyle{empty}
\begin{center}
	\begin{flushleft}
		\zhHeadType{论文题目：}
		\vspace{1.5em}
	\end{flushleft}
	\begin{center}
		\zihao{2}\heiti \bfseries
		\zh@title
	\end{center}

	\vspace{\stretch{1.2}}

	\zhHeadType{\expand[4em]{\authorName}\connSep}%
	\fillType{%
		\CJKunderline{\cexpand[\majorWidth\ccwd]{\zh@author}}%
	\quad}%
	\zhHeadType{\expand[4em]{\enrollDate}\connSep}%
	\fillType{%
		\CJKunderline{%
			\cexpand[\rightWidth\ccwd]{%
				\convertDate{\enroll@year}{\enroll@month}{}%
			}% cexpand
		}% underline
	}% fillType
	\\[1em]
	\zhHeadType{\expand[4em]{\majorName}\connSep}%
	\fillType{\showMajor\quad}%
	\zhHeadType{\expand[4em]{\fieldName}\connSep}%
	\fillType{\showField}%
	\\[1em]
	\zhHeadType{\expand[4em]{\superName}\connSep}%
	\fillType{%
		\begin{minipage}[t]{\majorWidth\ccwd}\supervisorList\end{minipage}%
		\quad%
	}% \CJKunderline{\cexpand[\majorWidth\ccwd]{\zh@supervisor}}%
	\zhHeadType{\expand[4em]{\profTitleName}\connSep}%
	\fillType{%
		\begin{minipage}[t]{\rightWidth\ccwd}\personTitleList\end{minipage}%
	}% \CJKunderline{\cexpand[\rightWidth\ccwd]{\zh@ptitle}}%
	\\

	\vfill
	\begin{flushleft}
		\zhHeadType{\submitDate\connSep}%
		\fillType{\CJKunderline{\cexpand[7em]{\useDate{small}}}}%
		\\[1em]%
		\zhHeadType{\defenseDate\connSep}%
		\fillType{%
			\CJKunderline{%
				\cexpand[7em]{%
					\convertDate{\defense@year}{\defense@month}{}%
				}% cexpand
			}% underline
		}% fillType
		\\[1em]%
		\zhHeadType{\awardDate\connSep}%
		\fillType{\CJKunderline{\cexpand[7em]{}}}%
		\vspace{3em}
	\end{flushleft}
\end{center}

\iftoggle{STabstract}{\clearpage}{%
	\newpage\thispagestyle{empty}\cleardoublepage%
}%
% Chinese.

%%%%%%%%%%%%%%%%%%%%%% English %%%%%%%%%%%%%%%%%%%%%%%%
\thispagestyle{empty}
\begin{center}
	\bfseries
	\begin{center}
		\Large
		% \linespread{2}\selectfont
		\MakeUppercase{\en@title}
	\end{center}
	\vfill
	{\small A Dissertation submitted in fulfillment of
	the requirements of the degree of}
	\vfill
	{\Large \MakeUppercase{ master of \type@degree}}
	\vfill
	{\small from}
	\vfill
	{\large \schoolNameEn}
	\vfill
	{\small by}
	\vfill
	{\large \en@author}
	\\\vspace{\stretch{2}}
	{\large \superNameEn: \en@ptitle \en@supervisor}
	\\\vspace{\stretch{4}}
	{\large \en@college}\\
	{\large \useDate{old}}
\end{center}
\iftoggle{STabstract}{\clearpage}{%
	\newpage\thispagestyle{empty}\cleardoublepage
}%
}% flypage

%%%%%%%%%%%%%%%%%% Affirmation %%%%%%%%%%%%%%%%%%%%%%
\newcommand{\affirmation}{
\thispagestyle{empty}
\begin{center}
	\vspace*{1em}
	{\ziju{2}\songti\zihao{-2}\bfseries \affirmationName}
	\vspace*{2em}
\end{center}

{% \songti\zihao{-4} % default
本人呈交给\schoolName{}的这篇\@degreeLevel\thesisType{}，%
除了所列参考文献和世所公认的文献外，%
全部是本人在导师指导下的研究成果。%
该论文资料尚没有呈交于其它任何学术机关作鉴定。%
}
\vfill
\begin{flushright}
	\expand[6em]{硕士生签名：}\hspace*{8em}% TODO, hard coded

	\expand[6em]{日期：}\hspace*{8em}
\end{flushright}
\vfill
\begin{center}
	\vspace*{2em}
	{\Large\bfseries \MakeUppercase{\affirmationNameEn}}
\end{center}

{\bfseries
I declare that this dissertation, submitted in fulfillment of the
requirements for the award of Master of \type@degree{} in \schoolNameEn{},
is wholly my own work unless referenced of acknowledge.
The document has not been submitted for qualification at any
other academic institute.
}
\vfill
\begin{flushright}
	\cexpand[6em]{\bfseries Signature:}\hspace*{8em}

	\cexpand[6em]{\bfseries Date:}\hspace*{8em}
\end{flushright}
\vspace{2em}
\newpage\thispagestyle{empty}\cleardoublepage
}

\endinput
% vim: set ts=4 sw=4 :

